const db = require('../models')

class ArticleRepository {
  async getAllArticles() {
    return await db.Article.findAll()
  }

  async findById(id) {
    return await db.Article.findByPk(id)
  }

  async create(articleData) {
    return await db.Article.create(articleData)
  }

  async update(id, articleData) {
    const article = await db.Article.findByPk(id)
    if (article) {
      return await article.update(articleData)
    }
    return null
  }

  async delArticleById(id) {
    // 如果查到对应id的文章数据就赋值给 article 否则赋 null
    const article = await db.Article.findByPk(id)
    if (article) {
      await article.destroy()
      return true
    }
    return false
  }
}

module.exports = new ArticleRepository()